<template>
  <div class="page-box">
    <div class="login">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" label-width="68px">
        <div style="text-align: center;" @click="$router.push('/login')">
          <img style="width: 97px; height: 91px;" src="../assets/ConsignmentFourzero/home/logo.png" alt="虞都互联" />
        </div>
        <h3 class="title" style="color: #fff;" @click="$router.push('/login')">
          虞都互联管理端
        </h3>
        <el-form-item label="账号：" prop="username" v-if="loginType == 'account'">
          <el-input name="account" v-model="loginForm.username" type="text" placeholder="请输入账号" class="transparent-input">
          </el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="password" v-if="loginType == 'account'">
          <el-input name="psd" v-model="loginForm.password" type="password" placeholder="请输入密码" @keyup.enter.native="handleLogin" class="transparent-input">
          </el-input>
        </el-form-item>
        <el-form-item prop="code" label="验证码：" v-if="captchaOnOff && loginType == 'account'">
          <el-input v-model="loginForm.code" placeholder="请输入验证码" style="width: 150px;" @keyup.enter.native="handleLogin" class="transparent-input">
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img" style="border-radius: 8px;" />
          </div>
        </el-form-item>
        <el-form-item label="手机号：" prop="phone" v-if="loginType == 'phone'">
          <el-input name="phone" v-model="loginForm.phone" type="text"  placeholder="请输入手机号">
          </el-input>
        </el-form-item>
        <el-form-item label="验证码：" prop="dxyzm" v-if="loginType == 'phone'">
          <el-input name="phonecode" v-model="loginForm.dxyzm"  placeholder="请输入短信验证码" style="width: 150px;" @keyup.enter.native="handleLogin">
          
          </el-input>
          <div class="login-code">
            <el-button type="primary"  :disabled="timer !== 0" @click="sendYzm">
              <span v-if="timer">{{ timer }}秒</span>
              <span v-else>发送验证码</span>
            </el-button>
          </div>
        </el-form-item>
        <el-checkbox v-if="loginType == 'account'" v-model="loginForm.rememberMe" style="margin: 0px 0px 25px 0px; color: #5b84ff;">记住密码</el-checkbox>
        <el-button :loading="loading" size="medium" type="primary" style="width: 100%; background-color: #5b84ff; border-radius: 10px;" @click.native.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div class="qiehuan" style="text-align:center">
          <el-button type="text" v-show="loginType=='phone'" @click="switchLogin('account')">密码登录</el-button>
          <el-button type="text" v-show="loginType=='account'" @click="switchLogin('phone')">验证码登录</el-button>
        </div>
        <div style="
            font-weight: 500;
            font-size: 12px;
            color: #ffffff;
            text-align: center;
            margin-top: 18px;
          ">
          Copyright © 2024-2028 yuduhulian.com
        </div>
      </el-form>
    </div>
    <!-- <div class="des">
      欢迎使用虞都互联,虞都互联 让每个商家都有一个属于自己的平台；
      虞都互联系统在Java语言的基础上，采用业内最先进的微服务架构理念，采用Spring
      Cloud微服务架构体系，为系统提供强有力的高并发、高可用、高存活的保证。
    </div> -->
    <div class="box">
      <div class="box_one">你好,欢迎使用虞都互联</div>
      <div class="box_two">[让每个商家都有一个属于自己的平台]</div>
      <!-- <div class="box_three">
        虞都互联系统在Java语言的基础上，采用业内最先进的微服务架构理念，采用Spring
        Cloud微服务架构体系，为系统提供强有力的高并发、高可用、高存活的保证。
      </div> -->
    </div>
  </div>
</template>

<script>
import { getCodeImg, sendSMSCode, getImToken } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import { getUserProfile } from "@/api/system/user";
import {   removeToken } from '@/utils/auth'
export default {
  name: "",
  data() {
    var phoneCheck = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("手机号不能为空"));
      } else if (!/^1[3-9]\d{9}$/.test(value)) {
        callback(new Error("手机号格式不正确"));
      } else {
        callback();
      }
    };
    return {
      loginType: "account",
      timer: 0,
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
        phone: "",
        yzmRankey: "",
        dxyzm: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
        phone: [{ validator: phoneCheck, trigger: "blur" }],
        dxyzm: [{ required: true, trigger: "blur", message: "请输入验证码" }],
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCode();
    
    // this.getCookie();
    localStorage.removeItem("EASEIM_loginUser");
    removeToken()
  },
  methods: {
    // 验证码相关 start
    switchLogin(txt) {
      this.loginType = txt;
      this.$refs.loginForm.clearValidate();
    },
    // 发送短信验证码
    sendYzm() {
      if (!/^1[3-9]\d{9}$/.test(this.loginForm.phone)) {
        this.$message({
          type: "error",
          message: "手机号错误！",
        });
        return;
      }
      this.timer = 60;
      this.changeTimer();
      let obj = {
        phone: this.loginForm.phone,
        type: "phoneLogin",
      };
      sendSMSCode(obj).then((res) => {
        this.$message({
          type: "success",
          message: res.msg,
        });
        this.loginForm.yzmRankey = res.data.yzmRankey;
      });
    },
    changeTimer() {
      if (this.timer > 0) {
        this.timer -= 1;
        setTimeout(() => {
          this.changeTimer();
        }, 1000);
      }
    },
    // 验证码相关end
    getCode() {
      getCodeImg().then((res) => {
        this.captchaOnOff =
          res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    // 登录IM
    async loginIM(userName) {
      let { msg } = await getImToken();
      let options = {
        user: userName,
        accessToken: msg,
      };
      window.localStorage.setItem(
        `EASEIM_loginUser`,
        JSON.stringify({ user: userName, accessToken: msg })
      );
      WebIM.conn.open(options);
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.loginForm.loginType = this.loginType;
          this.$store
            .dispatch("nidayeHehe", this.loginForm)
            .then(() => {
              getUserProfile().then((response) => {
               
                localStorage.setItem("userId", response.data.userId);
                // 登录成功 进行登录IM
                // this.loginIM(response.data.hxusername)
                this.$router
                  .push({ path: this.redirect || "/" })
                  .catch(() => {});
                // if (response.roleGroup.indexOf('商户中心') > -1) {
                //   console.log('零售客户')
                //   this.$router.push({ name: "Business/myAccount" }).catch(() => { });
                // } else {
                //   console.log('非零售客户')
                //   this.$router.push({ path: this.redirect || "/" }).catch(() => { });
                // }
              });
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaOnOff) {
                this.getCode();
              }
            });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.des {
  margin-top: 1000px;
}
.page-box {
  height: 100%;
  background-image: url("../assets/ConsignmentFourzero/home/loginbg.png");
  background-size: cover;
  padding-top: 30px;
  overflow-y: hidden;

  .w1200 {
    width: 1200px;
    margin: 0 auto;

    img {
      height: 50px;
      cursor: pointer;
    }
  }
}

.login {
  display: flex;
  justify-content: right;
  margin-right: 8.8%;
  align-items: center;
  height: calc(100% - 80px);
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: rgba(32, 39, 55, 0.8);
  width: 400px;
  height: 570px;
  // background-image: url('../assets/ConsignmentFourzero/system/logoBg.png');
  padding: 50px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 100px;
  height: 38px;
  float: right;
  margin-right: 11px;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.txt-box {
  margin-top: 15px;
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}

.box {
  width: 577px;
  height: 233px;
  // background-color: #fff;
  position: fixed;
  top: 35%;
  left: 8%;
  .box_one {
    height: 56px;
    line-height: 56px;
    font-family: STHeitiSC, STHeitiSC;
    font-weight: 500;
    font-size: 56px;
    color: #ffffff;
  }

  .box_two {
    height: 32px;
    line-height: 32px;
    font-family: SourceHanSansCN, SourceHanSansCN;
    font-weight: 800;
    font-size: 32px;
    color: #ffffff;
    padding-top: 28px;
  }

  .box_three {
    padding-top: 30px;
    width: 577px;
    height: 96px;
    font-family: STHeitiSC, STHeitiSC;
    font-weight: 500;
    font-size: 20px;
    color: #ffffff;
  }
}

::v-deep .el-form-item__error {
  padding-top: 10px;
}

::v-deep .el-input__inner {
  border: none !important;
  color: #fff;
}

::v-deep .el-form-item {
  border-bottom: 1px solid rgba(255, 255, 255, 0.3);
  padding-bottom: 10px;
}

::v-deep .el-form-item__label {
  text-align: left;
  color: #fff;
}

::v-deep .el-form-item__label::before {
  display: none;
}
</style>
